/**
 * Sangame.com Inc.
 * Copyright (c) 2006-2011 All Rights Reserved.
 */
package com.nmp.persistence.dao.weibo;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.nmp.persistence.dao.base.BaseJdbcDao;
import com.nmp.persistence.dao.base.Page;
import com.nmp.persistence.entity.WeiboAccount;

/**
 * 微博帐户相关操作
 * @author defier.lai
 * 2011-12-25 下午08:18:11
 * @version 1.0
 */
@Repository
public class WeiboAccountDao extends BaseJdbcDao {
	
	public void save(WeiboAccount account) {
		String sql = "insert into nmp_weibo_accounts (name, created_at) values (?, ?)";
		getJdbcTemplate().update(sql, new Object[]{});
	}
	
	
	public Page<WeiboAccount> findPage(int siteType, int pageSize, int currentPage) {
		StringBuilder condition = new StringBuilder();
		if(siteType > 0) {
			condition.append("account_type = "+siteType);
		} else {
			condition.append("1=1");
		}
		String countSql = "select count(1) from nmp_weibo_accounts where "+condition;
		long totalCount = getJdbcTemplate().queryForLong(countSql);
		Page<WeiboAccount> page = new Page<WeiboAccount>();
		page.setPageSize(pageSize);
		page.setTotalCount(totalCount);
		page.setPageNo(currentPage);
		
		String sql = "select * from nmp_weibo_accounts where "+ condition +" limit ?, ?";
		List<WeiboAccount> list = getJdbcTemplate().queryForList(sql, WeiboAccount.class, new Object[]{page.getFirst(), page.getPageSize()});
		
		page.setResult(list);
		return page;
	}

}
